package com.atguigu.gulimall.search.thread;

import java.util.Objects;
import java.util.concurrent.*;

/**
 * @Author: charls
 * @Description:TODO
 * @Date: 2023/05/10/ 17:54
 * @Version: 1.0
 */
public class CompletableFutureTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.....start.....");

        // 创建异步对象的四种方法：

        // 方法一：runAsync
        /*CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, executor);*/


        // 方法二：supplyAsync
        // 方法完成后的感知
        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).whenComplete((res,exception)->{
            System.out.println("异步任务成功完成了。。。结果是"+res+"；异常是："+exception);
        }).exceptionally(throwable -> {
            // 可以感知异常，同时返回默认值
            return 10;
        });*/

        // 方法完成后的处理
        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executor).handle((res,exc)->{
            if(res!=null){
                return res*2;
            }
            if(exc!=null){
                return 0;
            }
            return 0;
        });*/



        /**
         * 线程串行化
         * 1）、thenRun：不能获取到上一步的执行结果,无返回值
         * .thenRunAsync(()->{
         *         System.out.println("任务2启动了");
         *     }, executor);
         *
         * 2）、thenAccept：可以获取到上一步的执行结果，无返回值
         * .thenAcceptAsync(res->{
         *             System.out.println("任务2启动了--->"+res);
         *         },executor);
         *
         *3）、thenApply：可以获取到上一步的执行结果，有返回值
         * .thenApplyAsync(res -> {
         *             System.out.println("任务2启动了--->" + res);
         *             return res + "被我抓到了吧！";
         *         }, executor);
         */
        /*CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 5;
            System.out.println("运行结果" + i);
            return i;
        }, executor).thenApplyAsync(res -> {
            System.out.println("任务2启动了--->" + res);
            return res + "被我抓到了吧！";
        }, executor);*/



        /**
         * 两个任务都完成-》我们才能进行任务3
         * 1）、runAfterBoth不能感知前两个的结果，无返回值
         * future1.runAfterBothAsync(future2, ()->{
         *             System.out.println("任务3开始");
         *         },executor);
         *
         * 2）、thenAcceptBoth 可以感知到前两个任务的结果，无返回值
         * future1.thenAcceptBothAsync(future2,(f1,f2)->{
         *             System.out.println("任务3开始...两个任务的结果是"+f1+"---->"+f2);
         *         },executor);
         *
         * 3）、thenCombine 可以感知到前两个任务的结果,并且可以有返回值
         * CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
         *             System.out.println("任务3开始...");
         *             return f1 + ":" + f2 + "--->hello";
         *         }, executor);
         */
        /*CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程" + Thread.currentThread().getId());
            int i = 10 / 5;
            System.out.println("任务1结束" );
            return i;
        }, executor);
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程" + Thread.currentThread().getId());
            int i = 10 / 5;
            System.out.println("任务2结束" );
            return "Hello";
        }, executor);*/

       /* future1.runAfterBothAsync(future2, ()->{
            System.out.println("任务3开始");
        },executor);*/

       /* future1.thenAcceptBothAsync(future2,(f1,f2)->{
            System.out.println("任务3开始...两个任务的结果是"+f1+"---->"+f2);
        },executor);*/

        /*CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {
            System.out.println("任务3开始...");
            return f1 + ":" + f2 + "--->hello";
        }, executor);*/



        /**
         * 完成任意一个任务-》开始执行3号任务
         * 1）、runAfterEither：不感知结果，自己无返回值
         * future1.runAfterEitherAsync(future2,()->{
         *             System.out.println("任务3开始");
         *         },executor);
         *
         * 2）、acceptEither：可以感知结果，自己无返回值
         * future1.acceptEitherAsync(future2,(res)->{
         *             System.out.println("任务3开始"+res);
         *         },executor);
         *
         * 3）、applyToEither：可以感知结果，有返回值
         * CompletableFuture<Object> future = future1.applyToEitherAsync(future2, (res) -> {
         *             System.out.println("任务3开始");
         *             return res;
         *         }, executor);
         */
        /*CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程" + Thread.currentThread().getId());
            int i = 10 / 5;
            System.out.println("任务1结束" );
            return i;
        }, executor);
        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程" + Thread.currentThread().getId());
            try {
                Thread.sleep(3000);
                System.out.println("任务2结束" );
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "Hello";
        }, executor);*/

        /*future1.runAfterEitherAsync(future2,()->{
            System.out.println("任务3开始");
        },executor);*/

        /*future1.acceptEitherAsync(future2,(res)->{
            System.out.println("任务3开始"+res);
        },executor);*/

        /*CompletableFuture<Object> future = future1.applyToEitherAsync(future2, (res) -> {
            System.out.println("任务3开始");
            return res;
        }, executor);

        System.out.println("main.....end....."+future.get());
        */



        /**
         * 多任务组合
         */
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询图片的信息");
            return "hello.png";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询图片的属性");
            return "黑色+8GB+256GB";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询图片的介绍");
            return "华为";
        }, executor);

       /* CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        allOf.get();// get,必须等所有执行完毕才可以接着执行
        System.out.println("main---end-----"+futureImg.get()+futureDesc.get()+futureDesc.get());*/


        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        System.out.println("main---end----->返回值是"+anyOf.get());
    }
}
